# data_sources/yahoo_source.py
import yfinance as yf
import pandas as pd
from .base_data_source import BaseDataSource

class YahooDataSource(BaseDataSource):
    def __init__(self):
        super().__init__("yahoo")

    def get_stock_list(self) -> pd.DataFrame:
        """Yahoo Finance 主要适用于国际市场"""
        # 这里可以定义一些常见的国际股票
        international_stocks = [
            {'symbol': 'AAPL', 'name': 'Apple Inc', 'market': 'NASDAQ', 'country': 'US'},
            {'symbol': 'MSFT', 'name': 'Microsoft', 'market': 'NASDAQ', 'country': 'US'},
            {'symbol': 'GOOGL', 'name': 'Alphabet', 'market': 'NASDAQ', 'country': 'US'},
            {'symbol': 'TSLA', 'name': 'Tesla', 'market': 'NASDAQ', 'country': 'US'},
        ]
        return pd.DataFrame(international_stocks)

    def get_daily_data(self, symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
        try:
            stock = yf.Ticker(symbol)
            df = stock.history(start=start_date, end=end_date)

            if df.empty:
                return pd.DataFrame()

            df = df.reset_index()
            df = self.normalize_daily_data(df)
            df['symbol'] = symbol

            # 计算涨跌幅
            if 'change_rate' not in df.columns:
                df['change_rate'] = df['close_price'].pct_change() * 100

            return df

        except Exception as e:
            self.logger.error(f"Yahoo数据获取失败 {symbol}: {e}")
            return pd.DataFrame()